import proj4 from 'proj4'
proj4.defs([
  ['EPSG:30007', '+proj=tmerc +lat_0=0 +lon_0=118.5 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'],
  ['EPSG:4548', '+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'],
  ['EPSG:4549', '+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'],
  ['EPSG:4326', '+proj=longlat +datum=WGS84 +no_defs +type=crs'],
  [
    'EPSG:3857',
    '+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs'
  ],
  ['EPSG:4490', '+proj=longlat +ellps=GRS80 +no_defs']
])

const data = {
  g_lat: 0,
  g_lng: 0,
  address: '',
  w_lat: 0,
  w_lng: 0
}

var x_pi = (3.14159265358979324 * 3000.0) / 180.0
var pi = 3.1415926535897932384626 // π
var a = 6378245.0 // 长半轴
var ee = 0.00669342162296594323 // 扁率

function transformlat(lng, lat) {
  let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng))
  ret += ((20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0) / 3.0
  ret += ((20.0 * Math.sin(lat * pi) + 40.0 * Math.sin((lat / 3.0) * pi)) * 2.0) / 3.0
  ret += ((160.0 * Math.sin((lat / 12.0) * pi) + 320 * Math.sin((lat * pi) / 30.0)) * 2.0) / 3.0
  return ret
}

function transformlng(lng, lat) {
  let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng))
  ret += ((20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0) / 3.0
  ret += ((20.0 * Math.sin(lng * pi) + 40.0 * Math.sin((lng / 3.0) * pi)) * 2.0) / 3.0
  ret += ((150.0 * Math.sin((lng / 12.0) * pi) + 300.0 * Math.sin((lng / 30.0) * pi)) * 2.0) / 3.0
  return ret
}
/**
 * GCJ-02火星坐标系和WGS-84坐标系转换关系
 */

/**
 * GCJ02(火星坐标系) 转GPS84
 * @param {*} lng : 火星坐标系的经度
 * @param {*} lat : 火星坐标系纬度
 * @returns
 */
const gcj02towgs84 = (lng, lat) => {
  var dlat = transformlat(lng - 105.0, lat - 35.0)
  var dlng = transformlng(lng - 105.0, lat - 35.0)
  var radlat = (lat / 180.0) * pi
  var magic = Math.sin(radlat)
  magic = 1 - ee * magic * magic
  var sqrtmagic = Math.sqrt(magic)
  dlat = (dlat * 180.0) / (((a * (1 - ee)) / (magic * sqrtmagic)) * pi)
  dlng = (dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * pi)
  var mglat = lat + dlat
  var mglng = lng + dlng
  return [lng * 2 - mglng, lat * 2 - mglat]
}
/**
 *
 * @param {*} wgLat
 * @param {*} wgLon
 * @returns
 */
const wgs84togcj02 = (wgLat, wgLon) => {
  var mars_point = { lng: 0, lat: 0 }
  var dLat = transformlat(wgLon - 105.0, wgLat - 35.0)
  var dLon = transformlng(wgLon - 105.0, wgLat - 35.0)
  var radLat = (wgLat / 180.0) * pi
  var magic = Math.sin(radLat)
  magic = 1 - ee * magic * magic
  var sqrtMagic = Math.sqrt(magic)
  dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * pi)
  dLon = (dLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * pi)
  mars_point.lat = Number(wgLat) + Number(dLat)
  mars_point.lng = Number(wgLon) + Number(dLon)
  return mars_point
}
/**
 * GCJ-02火星坐标转BD-09百度坐标
 * @param {*} mars_point
 * @returns
 */
const marsTobaidu = (mars_point) => {
  var baidu_point = { lon: 0, lat: 0 }
  var x = mars_point.lon
  var y = mars_point.lat
  var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi)
  var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi)
  baidu_point.lon = z * Math.cos(theta) + 0.0065
  baidu_point.lat = z * Math.sin(theta) + 0.006
  return baidu_point
}
/**
 * BD-09百度坐标转GCJ-02火星坐标
 * @param {*} baidu_point
 * @returns
 */
const baiduTomars = (baidu_point) => {
  var mars_point = { lon: 0, lat: 0 }
  var x = baidu_point.lon - 0.0065
  var y = baidu_point.lat - 0.006
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi)
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi)
  mars_point.lon = z * Math.cos(theta)
  mars_point.lat = z * Math.sin(theta)
  return mars_point
}
const pt_xytowgs84 = (x, y) => {
  return proj4('EPSG:4549', 'EPSG:4326', [Number(x), Number(y)])
}
const wgs84toxy_pt = (lng, lat) => {
  return proj4('EPSG:4326', 'EPSG:4549', [Number(lng), Number(lat)])
}

/**
 * cesium 平面坐标转换
 */
export { gcj02towgs84, wgs84togcj02, marsTobaidu, baiduTomars, pt_xytowgs84, wgs84toxy_pt }
